from django.db import models
from datetime import datetime
from myapp.models import *
# Create your models here.
# 流程表
class ProcessModel(models.Model):
    process = models.CharField('流程名称',max_length=10,default="")
    create_time = models.DateTimeField('创建时间',default=datetime.now,max_length=100)

    def __str__(self):
        return self.process

    class Meta:
        db_table = "process_tb"
        verbose_name_plural = "流程表"

# 流程节点表
class PointModel(models.Model):
    point = models.CharField('节点名称',max_length=10,default="")
    process_id = models.ForeignKey(to=ProcessModel,on_delete=models.CASCADE,verbose_name='流程id')

    def __str__(self):
        return self.point

    class Meta:
        db_table = "point_tb"
        verbose_name_plural = "流程节点表"


# 样式表
class TypeModel(models.Model):
    type = models.CharField('样式名称', max_length=10, default="")
    way = models.CharField('样式代码',max_length=100,default='')
    def __str__(self):
        return self.type

    class Meta:
        db_table = "type_tb"
        verbose_name_plural = "样式表"

# 节点属性表
class AttrModel(models.Model):
    attr = models.CharField('属性名称',max_length=20,default='')
    type_id = models.ForeignKey(to=TypeModel,on_delete=models.CASCADE,verbose_name='样式表外键')

    def __str__(self):
        return self.attr

    class Meta:
        db_table = "attr_tb"
        verbose_name_plural = '节点属性表'

# 节点样式表
class PointLookModel(models.Model):
    process_id = models.ForeignKey(to=ProcessModel,on_delete=models.CASCADE,verbose_name='流程外键')
    point_id = models.ForeignKey(to=PointModel,on_delete=models.CASCADE,verbose_name='节点外键')
    attr_id = models.ForeignKey(to=AttrModel,on_delete=models.CASCADE,verbose_name='属性外键')

    def __str__(self):
        return self.attr_id.attr
    class Meta:
        db_table = 'point_look_tb'
        verbose_name_plural = '节点样式表'

# 节点属性实例表
class PointAttrModel(models.Model):
    process_id = models.ForeignKey(to=ProcessModel,on_delete=models.CASCADE,verbose_name='流程外键')
    point_id = models.ForeignKey(to=PointModel,on_delete=models.CASCADE,verbose_name='节点外键')
    user_id = models.ForeignKey(to=UserModel,on_delete=models.CASCADE,verbose_name='用户外键')
    attr = models.CharField('属性名称', max_length=200, default='')
    instance = models.CharField('实例名称',max_length=200,default='')

    def __str__(self):
        return self.instance

    class Meta:
        db_table = 'instance_tb'
        verbose_name_plural = '节点属性实例表'

# 流程实例表
class ProcessInstanceModel(models.Model):
    process_id = models.ForeignKey(to=ProcessModel,on_delete=models.CASCADE,verbose_name='流程id')
    user_id = models.ForeignKey(to=UserModel,on_delete=models.CASCADE,verbose_name='用户外键')
    createtime = models.DateTimeField('创建时间', default=datetime.now, max_length=100)

    def __str__(self):
        return self.id

    class Meta:
        db_table = "pro_ins_tb"
        verbose_name_plural = '流程实例表'

# 待办事项表
class WaitsModel(models.Model):
    process_instance_id = models.ForeignKey(to=ProcessInstanceModel,on_delete=models.CASCADE,verbose_name='流程实例表')
    new_time = models.CharField('最新时间',max_length=300,default=datetime.now)
    states = models.CharField('目前状态',max_length=5,default='未完成')

    def __str__(self):
        return self.id

    class Meta:
        db_table = 'waits_tb'
        verbose_name_plural = '待办事项表'

# 流程审批表
class ProcessAprovalModel(models.Model):
    process_instance_id = models.ForeignKey(to=ProcessModel,on_delete=models.CASCADE,verbose_name='流程实例id')
    username = models.CharField('用户名',max_length=10,default='')
    point_id = models.ForeignKey(to=PointModel,on_delete=models.CASCADE,verbose_name='节点id')
    act_time = models.CharField('操作时间',max_length=200,default=datetime.now)
    actor = models.ForeignKey(to=UserModel,on_delete=models.CASCADE,verbose_name='操作人id',default=1)
    point_state = models.CharField('节点状态',max_length=20,default='0 已提交 1 经理已审批 2 主管已审批 3 总监已审批 4 财务已审批')
    process_state = models.CharField('流程状态',max_length=20,default='0 未审批 1 已通过 2 已驳回')
    comments = models.CharField('批注',max_length=100,default='')

    def __str__(self):
        return self.actor

    class Meta:
        db_table = 'process_aproval_tb'
        verbose_name_plural = '流程审批表'








